package com.li.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.li.annotation.Log;
import com.li.enums.BusinessType;
import com.li.result.Result;
import com.li.service.SysRoleService;
import com.li.system.SysRole;
import com.li.vo.AssginRoleVo;
import com.li.vo.SysRoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Author: lyh
 * @Date: 2022/10/28
 * @Time: 19:08
 * @Description:
 */
@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    //查询所有记录
    @ApiOperation("查询所有接口")
    @GetMapping("/findAll")
    public Result findAllRole() {

        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查询")
    @GetMapping("/findRoleById/{id}")
    public Result list(@PathVariable Long id) {
        SysRole sysRole = sysRoleService.getById(id);
        return Result.ok(sysRole);
    }

    //逻辑删除
    @Log(title = "角色管理(删除)",businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("逻辑删除接口")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean remove = sysRoleService.removeById(id);
        if (remove) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @Log(title = "角色管理(批量删除)",businessType = BusinessType.DELETE)
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        boolean remove = sysRoleService.removeByIds(ids);
        if (remove) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @Log(title = "角色管理(添加)",businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation("添加接口")
    @PostMapping("/save")
    public Result saveSysRole(@RequestBody SysRole sysRole) {
        boolean save = sysRoleService.save(sysRole);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @Log(title = "角色管理(修改)",businessType = BusinessType.UPDATE)
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("修改接口")
    @PostMapping("/update")
    public Result update(@RequestBody SysRole sysRole) {

        boolean update = sysRoleService.updateById(sysRole);
        if (update) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页")
    @GetMapping("{page}/{limit}")
    public Result pageQueryRole(@PathVariable Long page, @PathVariable Long limit,
                                SysRoleQueryVo sysRoleQueryVo) {
        //创建page对象
        Page<SysRole> pageParam = new Page<>(page, limit);
        IPage<SysRole> pageModel = sysRoleService.selectPage(pageParam, sysRoleQueryVo);
        return Result.ok(pageModel);
    }


    @ApiOperation("获取用户的角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable String userId) {
        Map<String, Object> map = sysRoleService.getRoleByUserId(userId);
        return Result.ok(map);
    }

    @PreAuthorize("hasAuthority('bnt.sysUser.assignRole')")
    @ApiOperation("给用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
       sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }

}
